package com.lgx.mobile.rest.friend;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lgx.mobile.entity.Friend;
import com.lgx.mobile.model.FriendModel;
import com.lgx.mobile.service.IFriendService;
import com.lgx.right.entity.User;
import com.lgx.right.service.IUserService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/getFriend")
public class GetFriendController {
	
	private static final Logger logger = Logger.getLogger(GetFriendController.class);

	@Autowired
	IFriendService friendService;

	public IFriendService getFriendService() {
		return friendService;
	}
	
	@Autowired
	IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}

	/**
	 * 乐友查询
	 * @param onwerUserId onwer_user_id is key
	 * @return 所属用户所有好友信息集合（List）；0：该用户还没有添加乐友；1：参数异常，有参数为空
	 */
	@RequestMapping(value="/{onwer_user_id}",method=RequestMethod.GET)
	@ResponseBody
	public Object getFriend(@PathVariable("onwer_user_id") String onwerUserId){
		if(StringUtils.isNotBlank(onwerUserId)){
			List<Friend> friendList = getFriendService().findFriendListByOnwerUserId(Long.parseLong(onwerUserId));
			List<FriendModel> friendModelList = new ArrayList<FriendModel>();
			User onwerUser = getUserService().findUserById(Long.parseLong(onwerUserId));
			String onwerTrueName = onwerUser.getTruename();
			if(friendList.size() != 0){
				for(int i=0;i<friendList.size();i++){
					User friendUser = getUserService().findUserById(friendList.get(i).getFriendUserId());
					
					FriendModel friendModel = new FriendModel(null);
					friendModel.setId(friendList.get(i).getId());
					friendModel.setOnwerUserId(friendList.get(i).getOnwerUserId());
					friendModel.setOnwerTrueName(onwerTrueName);
					friendModel.setFriendUserId(friendList.get(i).getFriendUserId());
					friendModel.setFriendTrueName(friendUser.getTruename());
					friendModel.setType(friendList.get(i).getType());
					friendModelList.add(friendModel);
				}
				return friendModelList;
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("getFriend faild onwerUserId is error");
				}
//				"getFriend faild please check onwerUserId is exists";
				return "0";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("getFriend faild onwerUserId is not filled");
			}
//		    "getFriend faild please check onwerUserId is filled";
			return "1";
		}
	}
}
